# -*- autoconf -*-
#########################################
##
# Checks for types
##
#########################################

##
#   Standard checks:
##

# Checks for typedefs, structures, and compiler characteristics.
AC_TYPE_OFF_T
AC_TYPE_PID_T
AC_HEADER_TIME
AC_COMPILE_IFELSE(
    [AC_LANG_PROGRAM([],
        [int x __attribute__((deprecated))])],
    [attr="__attribute__((deprecated))"],
    [attr="/**/"])
AC_DEFINE_UNQUOTED([NETSNMP_ATTRIBUTE_DEPRECATED], [${attr}],
          [Used to make the compiler issue a warning about deprecated functions and variables])

AC_COMPILE_IFELSE(
    [AC_LANG_PROGRAM([],
        [int x __attribute__((unused))])],
    [attr="__attribute__((unused))"],
    [attr="/**/"])
AC_DEFINE_UNQUOTED([NETSNMP_ATTRIBUTE_UNUSED], [${attr}],
          [Used to suppress compiler warnings about unused functions and variables])


##
#   More complex checks:
##

#   Check for 'socklen_t', 'in_addr_t' and 'ssize_t'.
#
AC_CHECK_TYPES([socklen_t, in_addr_t, ssize_t],,,[
#if defined(HAVE_WINSOCK2_H) && defined(HAVE_WS2TCPIP_H)
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
#include <stdlib.h>
#include <stddef.h>
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif])

if test "x$ac_cv_type_in_addr_t" != xyes; then
AC_DEFINE([in_addr_t], [unsigned long],
          [in_addr_t definition if not defined in <netinet/in.h>. Must match the type of sockaddr_in::sin_addr])
fi

#
# Determine the type of fd_set::fds_bits on Unix systems.
#

netsnmp_save_CFLAGS=$CFLAGS
CFLAGS="$CFLAGS -Werror"

AC_MSG_CHECKING([for the type of fd_set::fds_bits])
for type in __fd_mask __int32_t long 'unsigned long' unknown; do
  AC_COMPILE_IFELSE([AC_LANG_PROGRAM([
#include <sys/select.h>
#include <stddef.h>
#include <string.h>
    ], [
      fd_set set;
      memset(&set, 0, sizeof(set));
      {
        ${type} *fds_array = set.fds_bits;
        return fds_array != NULL;
      }
    ])], [break])
done
AC_MSG_RESULT(${type})
AC_DEFINE_UNQUOTED([NETSNMP_FD_MASK_TYPE], [${type}],
                   [Type of fd_set::fds_bits])

CFLAGS=$netsnmp_save_CFLAGS

#
# Check for the type of the fifth argument of select()
#

netsnmp_save_CFLAGS=$CFLAGS
CFLAGS="$CFLAGS -Werror"

AC_MSG_CHECKING([for the type of the fifth argument of select()])
arg_type="struct timeval"
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [])], [
# The compiler supports -Werror.
for t in "struct timeval" "struct __ms_timeval"; do
AC_COMPILE_IFELSE(
    [AC_LANG_PROGRAM([
#if defined(HAVE_WINSOCK2_H) && defined(HAVE_WS2TCPIP_H)
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#endif
#include <stddef.h>
], [return select(0, NULL, NULL, NULL, ($t *)NULL)])],
    [arg_type=$t; break])
done
], [
: # The compiler does not support -Werror.
])
AC_MSG_RESULT(${arg_type})
AC_DEFINE_UNQUOTED([NETSNMP_SELECT_TIMEVAL], [${arg_type}],
        [Type of the fifth argument of select()])

AC_MSG_CHECKING([for the type of the third argument of ioctlsocket()])
arg_type=unknown
AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [])], [
# The compiler supports -Werror.
for t in "unsigned int" "unsigned long"; do
AC_COMPILE_IFELSE(
    [AC_LANG_PROGRAM([
#if defined(HAVE_WINSOCK2_H) && defined(HAVE_WS2TCPIP_H)
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif
#endif
#include <stddef.h>
], [return ioctlsocket(0, 0, ($t *)NULL)])],
    [arg_type=$t; break])
done
], [
: # The compiler does not support -Werror.
])
AC_MSG_RESULT(${arg_type})
AC_DEFINE_UNQUOTED([NETSNMP_IOCTLSOCKET_ARG], [${arg_type}],
        [Type of the third argument of ioctlsocket()])

CFLAGS=$netsnmp_save_CFLAGS
